package br.jcrsistemas.componentes;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.text.ParseException;

import javax.swing.JFormattedTextField;
import javax.swing.JTextField;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.MaskFormatter;

public class SysMaskTextField extends JFormattedTextField {

    /**
     * 
     */
    private static final long serialVersionUID = -2701761001153841771L;
    private boolean obrigatorio;
    private Component nextComponente;
    private String mascara = "";
    boolean mostraUnderline = false;
    private MaskFormatter fMascara = new MaskFormatter();
    private boolean enterChangeFocus = true;

    public SysMaskTextField(String mascara, boolean mostraUnderline) throws ParseException {
        this.addFocusListener(new ActionFoco());

        MaskFormatter fMascara = new MaskFormatter();
        fMascara.setMask(mascara);

        fMascara.setPlaceholderCharacter((mostraUnderline ? '_' : ' '));
        setObrigatorio(false);
//		setInputVerifier(new SisComInputVerifier());
        setFocusLostBehavior(this.COMMIT);


    }

    public SysMaskTextField(DefaultFormatterFactory formatador) {

        setFormatterFactory(formatador);


    }

    public SysMaskTextField() {
        super();
        this.addFocusListener(new ActionFoco());
//		setInputVerifier(new SisComInputVerifier());
        setFocusLostBehavior(this.COMMIT);
        addKeyListener(new KeyAdapter() {

            @Override
            public void keyPressed(KeyEvent e) {
                if(e.getKeyCode() == KeyEvent.VK_ENTER && enterChangeFocus) {
                    transferFocus();
                }
            }
            
        });
    }

    public void setEnterChangeFocus(boolean enterChangeFocus) {
        this.enterChangeFocus = enterChangeFocus;
    }

    public boolean getEnterChangeFocus() {
        return this.enterChangeFocus;
    }

    public SysMaskTextField(String mascara) throws ParseException {
        this(mascara, true);
    }

    public void setObrigatorio(boolean obrigatorio) {
        this.obrigatorio = obrigatorio;
    }

    public boolean isObrigatorio() {
        return obrigatorio;
    }

    public void setNextComponente(Component nextComponente) {
        this.nextComponente = nextComponente;
    }

    public Component getNextComponente() {
        return nextComponente;
    }

    public void setMascara(String mascara) throws ParseException {
        this.mascara = mascara;

        fMascara.setMask(mascara);
        fMascara.setPlaceholderCharacter((mostraUnderline ? '_' : ' '));
        setFormatter(fMascara);

    }

    public String getMascara() {
        return mascara;
    }

    public void setMostraUnderline(boolean mostraUnderline) {
        this.mostraUnderline = mostraUnderline;
        fMascara.setPlaceholderCharacter((mostraUnderline ? '_' : ' '));
        setFormatter(fMascara);
    }

    public boolean isMostraUnderline() {
        return mostraUnderline;
    }

    public class ActionFoco implements FocusListener {

        @Override
        public void focusGained(FocusEvent arg0) {
            JTextField componente = (JTextField) arg0.getComponent();
            componente.setBackground(new Color(Integer.parseInt("FFE87C", 16)));

        }

        @Override
        public void focusLost(FocusEvent arg0) {
            JTextField componente = (JTextField) arg0.getComponent();

            componente.setBackground(Color.WHITE);
            if (getNextComponente() != null) {
                getNextComponente().requestFocus();
            }

        }
    }
}
